import os
from PIL import Image
import glob

def convert_to_png():
    """将sortpic目录中的webp图片转换为png格式"""
    # 确保sortpic目录存在
    if not os.path.exists('sortpic'):
        print("❌ 错误：找不到sortpic目录")
        return

    # 获取所有webp文件
    webp_files = glob.glob('sortpic/*.webp')
    
    if not webp_files:
        print("没有找到webp文件")
        return
        
    print(f"找到 {len(webp_files)} 个webp文件")

    # 转换每个文件
    for webp_file in webp_files:
        try:
            # 打开webp文件
            with Image.open(webp_file) as img:
                # 构建新的文件名
                png_file = os.path.splitext(webp_file)[0] + '.png'
                
                # 转换并保存为PNG
                img.save(png_file, 'PNG')
                print(f"✅ 已转换: {os.path.basename(webp_file)} -> {os.path.basename(png_file)}")
                
                # 删除原webp文件
                os.remove(webp_file)
                print(f"✅ 已删除原文件: {os.path.basename(webp_file)}")
                
        except Exception as e:
            print(f"❌ 处理 {webp_file} 时出错: {str(e)}")

if __name__ == "__main__":
    convert_to_png() 